package com.ming.luckyDemo.service.serviceImpl;

import com.ming.luckyDemo.mapper.QueryLuckyMapper;
import com.ming.luckyDemo.pojo.DO.BetBillDO;
import com.ming.luckyDemo.pojo.DO.HistoryLuckyDO;
import com.ming.luckyDemo.pojo.DO.UserInfoDO;
import com.ming.luckyDemo.pojo.TDO.BetDTO;
import com.ming.luckyDemo.service.QueryLuckyService;
import com.ming.luckyDemo.util.DateUtil;
import com.ming.luckyDemo.util.NumberUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

@SuppressWarnings("all")
@Service
public class QueryLuckyServiceImpl implements QueryLuckyService {

    @Autowired
    private QueryLuckyMapper queryLuckyMapper;

    BetDTO betDTO = new BetDTO();

    @Override
    public BetDTO setBetDTO(int numbers) {

        betDTO.setNumbers(numbers);

        double zBig = queryLuckyMapper.queryZBig(numbers);
        if(zBig>0){
            betDTO.setZBig(zBig);
        }else {
            betDTO.setZBig(0);
        }
        double zSmall = queryLuckyMapper.queryZSmall(numbers);
        if (zSmall>0) {
            betDTO.setZSmall(zSmall);
        }else {
            betDTO.setZSmall(0);
        }

        double zOdd = queryLuckyMapper.queryZOdd(numbers);
        if (zOdd>0) {
            betDTO.setZOdd(zOdd);
        }else {
            betDTO.setZOdd(0);
        }
        double zEven = queryLuckyMapper.queryZEven(numbers);
        if (zEven>0){
            betDTO.setZEven(zEven);
        }else {
            betDTO.setZEven(0);
        }


        betDTO.setBig1(queryLuckyMapper.queryBig1(numbers));
        betDTO.setSmall1(queryLuckyMapper.querySmall1(numbers));
        betDTO.setOdd1(queryLuckyMapper.queryOdd1(numbers));
        betDTO.setEven1(queryLuckyMapper.queryeven1(numbers));

        betDTO.setBig2(queryLuckyMapper.queryBig2(numbers));
        betDTO.setSmall2(queryLuckyMapper.querySmall2(numbers));
        betDTO.setOdd2(queryLuckyMapper.queryOdd2(numbers));
        betDTO.setEven2(queryLuckyMapper.queryeven2(numbers));

        betDTO.setBig3(queryLuckyMapper.queryBig3(numbers));
        betDTO.setSmall3(queryLuckyMapper.querySmall3(numbers));
        betDTO.setOdd3(queryLuckyMapper.queryOdd3(numbers));
        betDTO.setEven3(queryLuckyMapper.queryeven3(numbers));

        System.out.println(betDTO);

        return betDTO;
    }

    @Override
    public UserInfoDO login2(String cookieName) {
        return queryLuckyMapper.queryByName(cookieName);
    }

    @Override
    public void setLucky(BetDTO dto) {
        int[] Natural  = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
        HashMap<String, Integer> map = new HashMap<String, Integer>();
        int[] bigBall1;
        int[] oddBall1;
        int[] bigBall2;
        int[] oddBall2;
        int[] bigBall3;
        int[] oddBall3;

//        while (true){
//            if (dto.getSize1()>0){
//                bigBall1 = new int[] {5,6,7,8,9};
//            }else if (dto.getParity1()>0){
//                oddBall1 = new int[] {1,3,5,7,9};
//            }else if (dto.getSize2()>0){
//                bigBall2 = new int[] {5,6,7,8,9};
//            }else if (dto.getParity2()>0){
//                oddBall2 = new int[] {1,3,5,7,9};
//            }else if (dto.getSize3()>0){
//                bigBall3 = new int[] {5,6,7,8,9};
//            }else if (dto.getParity3()>0){
//                oddBall3 = new int[] {1,3,5,7,9};
//            }
//        }
//        int size1 = dto.getSize1()>0?1:0;
//        switch (size1){
//            case 1:
//                bigBall1 = new int[] {5,6,7,8,9};
//                break;
//        }

    }

    @Override
    public double[] seekLucky(BetDTO dto) {

        System.out.println("总大："+dto.getZSize()+" || "+dto.getZParity());
        System.out.println("球1："+dto.getSize1()+" || "+dto.getParity1());
        System.out.println("球2："+dto.getSize2()+" || "+dto.getParity2());
        System.out.println("球3："+dto.getSize3()+" || "+dto.getParity3());
        //负数，网站收益
        double[] max = new double[]{0.0,0.0,0.0};

        double[] result = new double[6];

        NumberFormat nf = NumberFormat.getNumberInstance();
        nf.setMaximumFractionDigits(2);

        for (int i = 0; i < 1000; i++) {
            //总赔付的金额
            double count = 0;
            if (i<10){
                int a = 0;
                int b = 0;
                count = count - dto.getSize1();
                count = count - dto.getParity1();
                count = count - dto.getSize2();
                count = count - dto.getParity2();
                switch (i){
                    case 0:
                        count = count - dto.getSize3();
                        count = count - dto.getParity3();
                        break;
                    case 1:
                        count = count - dto.getSize3();
                        count = count + dto.getParity3();
                        break;
                    case 2:
                        count = count - dto.getSize3();
                        count = count - dto.getParity3();
                        break;
                    case 3:
                        count = count - dto.getSize3();
                        count = count + dto.getParity3();
                        break;
                    case 4:
                        count = count - dto.getSize3();
                        count = count - dto.getParity3();
                        break;
                    case 5:
                        count = count + dto.getSize3();
                        count = count + dto.getParity3();
                        break;
                    case 6:
                        count = count + dto.getSize3();
                        count = count - dto.getParity3();
                        break;
                    case 7:
                        count = count + dto.getSize3();
                        count = count + dto.getParity3();
                        break;
                    case 8:
                        count = count + dto.getSize3();
                        count = count - dto.getParity3();
                        break;
                    case 9:
                        count = count + dto.getSize3();
                        count = count + dto.getParity3();
                        break;
                    default:
                        break;
                }
                int z=a+b+i;
                switch (z){
                    case 0: case 2: case 4: case 6: case 8: case 10: case 12:
                        count = count - dto.getZSize();
                        count = count - dto.getZParity();
                        break;
                    case 1: case 3: case 5: case 7: case 9: case 11: case 13:
                        count = count - dto.getZSize();
                        count = count + dto.getZParity();
                        break;
                    case 14: case 16: case 18: case 20: case 22: case 24: case 26:
                        count = count + dto.getZSize();
                        count = count - dto.getZParity();
                        break;
                    case 15: case 17: case 19: case 21: case 23: case 25: case 27:
                        count = count + dto.getZSize();
                        count = count + dto.getZParity();
                        break;
                    default:
                        break;
                }
//                System.out.println(0+0+i+"总亏损金额："+count);

                int profit = getProfit(count,max);
                if (profit>0){
                    result[profit+1-2]=i;
                    result[profit+2]=count;
                }
            }else if (i>9 && i<100){
                int a = 0;
                int b = i/10;
                int c = i-b*10;
                count = count - dto.getBig1();
                count = count - dto.getParity1();
                switch (b){
                    case 0:
                        count = count - dto.getSize2();
                        count = count - dto.getParity2();
                        break;
                    case 1:
                        count = count - dto.getSize2();
                        count = count + dto.getParity2();
                        break;
                    case 2:
                        count = count - dto.getSize2();
                        count = count - dto.getParity2();
                        break;
                    case 3:
                        count = count - dto.getSize2();
                        count = count + dto.getParity2();
                        break;
                    case 4:
                        count = count - dto.getSize2();
                        count = count - dto.getParity2();
                        break;
                    case 5:
                        count = count + dto.getSize2();
                        count = count + dto.getParity2();
                        break;
                    case 6:
                        count = count + dto.getSize2();
                        count = count - dto.getParity2();
                        break;
                    case 7:
                        count = count + dto.getSize2();
                        count = count + dto.getParity2();
                        break;
                    case 8:
                        count = count + dto.getSize2();
                        count = count - dto.getParity2();
                        break;
                    case 9:
                        count = count + dto.getSize2();
                        count = count + dto.getParity2();
                        break;
                    default:
                        break;
                }
                switch (c){
                    case 0:
                        count = count - dto.getSize3();
                        count = count - dto.getParity3();
                        break;
                    case 1:
                        count = count - dto.getSize3();
                        count = count + dto.getParity3();
                        break;
                    case 2:
                        count = count - dto.getSize3();
                        count = count - dto.getParity3();
                        break;
                    case 3:
                        count = count - dto.getSize3();
                        count = count + dto.getParity3();
                        break;
                    case 4:
                        count = count - dto.getSize3();
                        count = count - dto.getParity3();
                        break;
                    case 5:
                        count = count + dto.getSize3();
                        count = count + dto.getParity3();
                        break;
                    case 6:
                        count = count + dto.getSize3();
                        count = count - dto.getParity3();
                        break;
                    case 7:
                        count = count + dto.getSize3();
                        count = count + dto.getParity3();
                        break;
                    case 8:
                        count = count + dto.getSize3();
                        count = count - dto.getParity3();
                        break;
                    case 9:
                        count = count + dto.getSize3();
                        count = count + dto.getParity3();
                        break;
                    default:
                        break;
                }
                int z=a+b+c;
                switch (z){
                    case 0: case 2: case 4: case 6: case 8: case 10: case 12:
                        count = count - dto.getZSize();
                        count = count - dto.getZParity();
                        break;
                    case 1: case 3: case 5: case 7: case 9: case 11: case 13:
                        count = count - dto.getZSize();
                        count = count + dto.getZParity();
                        break;
                    case 14: case 16: case 18: case 20: case 22: case 24: case 26:
                        count = count + dto.getZSize();
                        count = count - dto.getZParity();
                        break;
                    case 15: case 17: case 19: case 21: case 23: case 25: case 27:
                        count = count + dto.getZSize();
                        count = count + dto.getZParity();
                        break;
                    default:
                        break;
                }
//                System.out.println(i+"总亏损的金额："+count);

                int profit = getProfit(count,max);
                if (profit>0){
                    result[profit-1]=i;
                    result[profit+2]=count;
                }

            }else {
                int a = i/100;
                int b = (i-a*100)/10;
                int c = i-a*100-b*10;

                switch (a){
                    case 1:
                        count = count - dto.getSize1();
                        count = count + dto.getParity1();
                        break;
                    case 2:
                        count = count - dto.getSize1();
                        count = count - dto.getParity1();
                        break;
                    case 3:
                        count = count - dto.getSize1();
                        count = count + dto.getParity1();
                        break;
                    case 4:
                        count = count - dto.getSize1();
                        count = count - dto.getParity1();
                        break;
                    case 5:
                        count = count + dto.getSize1();
                        count = count + dto.getParity1();
                        break;
                    case 6:
                        count = count + dto.getSize1();
                        count = count - dto.getParity1();
                        break;
                    case 7:
                        count = count + dto.getSize1();
                        count = count + dto.getParity1();
                        break;
                    case 8:
                        count = count + dto.getSize1();
                        count = count - dto.getParity1();
                        break;
                    case 9:
                        count = count + dto.getSize1();
                        count = count + dto.getParity1();
                        break;
                    default:
                        break;
                }
                switch (b){
                    case 0:
                        count = count - dto.getSize2();
                        count = count - dto.getParity2();
                        break;
                    case 1:
                        count = count - dto.getSize2();
                        count = count + dto.getParity2();
                        break;
                    case 2:
                        count = count - dto.getSize2();
                        count = count - dto.getParity2();
                        break;
                    case 3:
                        count = count - dto.getSize2();
                        count = count + dto.getParity2();
                        break;
                    case 4:
                        count = count - dto.getSize2();
                        count = count - dto.getParity2();
                        break;
                    case 5:
                        count = count + dto.getSize2();
                        count = count + dto.getParity2();
                        break;
                    case 6:
                        count = count + dto.getSize2();
                        count = count - dto.getParity2();
                        break;
                    case 7:
                        count = count + dto.getSize2();
                        count = count + dto.getParity2();
                        break;
                    case 8:
                        count = count + dto.getSize2();
                        count = count - dto.getParity2();
                        break;
                    case 9:
                        count = count + dto.getSize2();
                        count = count + dto.getParity2();
                        break;
                    default:
                        break;
                }
                switch (c){
                    case 0:
                        count = count - dto.getSize3();
                        count = count - dto.getParity3();
                        break;
                    case 1:
                        count = count - dto.getSize3();
                        count = count + dto.getParity3();
                        break;
                    case 2:
                        count = count - dto.getSize3();
                        count = count - dto.getParity3();
                        break;
                    case 3:
                        count = count - dto.getSize3();
                        count = count + dto.getParity3();
                        break;
                    case 4:
                        count = count - dto.getSize3();
                        count = count - dto.getParity3();
                        break;
                    case 5:
                        count = count + dto.getSize3();
                        count = count + dto.getParity3();
                        break;
                    case 6:
                        count = count + dto.getSize3();
                        count = count - dto.getParity3();
                        break;
                    case 7:
                        count = count + dto.getSize3();
                        count = count + dto.getParity3();
                        break;
                    case 8:
                        count = count + dto.getSize3();
                        count = count - dto.getParity3();
                        break;
                    case 9:
                        count = count + dto.getSize3();
                        count = count + dto.getParity3();
                        break;
                    default:
                        break;
                }
                int z=a+b+c;
                switch (z){
                    case 0: case 2: case 4: case 6: case 8: case 10: case 12:
                        count = count - dto.getZSize();
                        count = count - dto.getZParity();
                        break;
                    case 1: case 3: case 5: case 7: case 9: case 11: case 13:
                        count = count - dto.getZSize();
                        count = count + dto.getZParity();
                        break;
                    case 14: case 16: case 18: case 20: case 22: case 24: case 26:
                        count = count + dto.getZSize();
                        count = count - dto.getZParity();
                        break;
                    case 15: case 17: case 19: case 21: case 23: case 25: case 27:
                        count = count + dto.getZSize();
                        count = count + dto.getZParity();
                        break;
                    default:
                        break;
                }
//                System.out.println(i+"总亏损的金额："+count);

                int profit = getProfit(count,max);
                if (profit>0){
                    result[profit-1]=i;
                    result[profit+2]=count;
                }
            }
        }
        NumberUtil numberUtil = new NumberUtil();
        String digit = numberUtil.getDigit((int) result[0]);
        DateUtil dateUtil = new DateUtil();

//        将最优解存入数据库
//        queryLuckyMapper.saveLucky(dto.getNumbers(),digit,dateUtil.getCurrentTime());
        System.out.println(Arrays.toString(result));
        return result;
    }

    @Override
    public String getRandom() {

        int a = (int)(1+Math.random()*(1000-1+1));

        NumberUtil numberUtil = new NumberUtil();
        String str = numberUtil.getDigit(a);
        System.out.println(str);
        return str;
    }
    @Override
    public void saveLucky(int numbers,String lucky) {
        DateUtil dateUtil = new DateUtil();
        queryLuckyMapper.saveLucky(numbers,lucky,dateUtil.getCurrentTime());
    }

    @Override
    public void updateBalance(String username,int numbers) {
        List<BetBillDO> betBillDOS = queryLuckyMapper.selectBetByName(username, numbers);

        HistoryLuckyDO historyLuckyDO = queryLuckyMapper.selectLuckyByNumbers(numbers);

        for (BetBillDO betBillDO : betBillDOS) {

            double balance = queryLuckyMapper.selectByInfo(username);
            double bet = betBillDO.getBet();

            switch(betBillDO.getSite()){
                case "总和":
                    if ("大".equals(betBillDO.getType()) && getSum(historyLuckyDO.getLucky())>13
                            || "小".equals(betBillDO.getType()) && getSum(historyLuckyDO.getLucky())<14
                            || "单".equals(betBillDO.getType()) && getSum(historyLuckyDO.getLucky())%2 != 0
                            || "双".equals(betBillDO.getType()) && getSum(historyLuckyDO.getLucky())%2==0){
                        double newBalance = balance+bet*2;
                        queryLuckyMapper.updateBalance(newBalance,username);
                        break;
                    }
                    break;
                case "数字1":
                    if ("大".equals(betBillDO.getType()) && getA(historyLuckyDO.getLucky())>4
                            || "小".equals(betBillDO.getType()) && getA(historyLuckyDO.getLucky())<5
                            || "单".equals(betBillDO.getType()) && getA(historyLuckyDO.getLucky())%2 != 0
                            || "双".equals(betBillDO.getType()) && getA(historyLuckyDO.getLucky())%2==0){
                        double newBalance = balance+bet*2;
                        queryLuckyMapper.updateBalance(newBalance,username);
                        break;
                    }
                    break;
                case "数字2":
                    if ("大".equals(betBillDO.getType()) && getB(historyLuckyDO.getLucky())>4
                            || "小".equals(betBillDO.getType()) && getB(historyLuckyDO.getLucky())<5
                            || "单".equals(betBillDO.getType()) && getB(historyLuckyDO.getLucky())%2 != 0
                            || "双".equals(betBillDO.getType()) && getB(historyLuckyDO.getLucky())%2==0){
                        double newBalance = balance+bet*2;
                        queryLuckyMapper.updateBalance(newBalance,username);
                        break;
                    }
                    break;
                case "数字3":
                    if ("大".equals(betBillDO.getType()) && getC(historyLuckyDO.getLucky())>4
                            || "小".equals(betBillDO.getType()) && getC(historyLuckyDO.getLucky())<5
                            || "单".equals(betBillDO.getType()) && getC(historyLuckyDO.getLucky())%2 != 0
                            || "双".equals(betBillDO.getType()) && getC(historyLuckyDO.getLucky())%2==0){
                        double newBalance = balance+bet*2;
                        queryLuckyMapper.updateBalance(newBalance,username);
                        break;
                    }
                    break;
                default:
                    break;
            }
        }
    }

    @Override
    public List<String> getName() {
        List<String> strings = queryLuckyMapper.selectUserName();
        return strings;
    }

    public int getProfit(double count, double[] max){

        //正数，网站亏损
        double[] min = new double[3];

        if (count<=max[2]){
            if (count<max[1]){
                if (count<=max[0]){
                    max[2]=max[1];
                    max[1]=max[0];
                    max[0]=count;
                    return 1;
                }
                max[2]=max[1];
                max[1]=count;
                return 2;
            }
            max[2]=count;
            return 3;
        }
        return 0;
    }

    public int getSum(String x){
        String a = x.substring(0, 1);
        String b = x.substring(1, 2);
        String c = x.substring(2, 3);
        int sum = 0;
        sum = Integer.parseInt(a) + Integer.parseInt(b) + Integer.parseInt(c);
        return sum;
    }

    public int getA(String x) {
        String a = x.substring(0,1);
        int sum = Integer.parseInt(a);
        return sum;
    }
    public int getB(String x) {
        String b = x.substring(1,2);
        int sum = Integer.parseInt(b);
        return sum;
    }
    public int getC(String x) {
        String c = x.substring(2,3);
        int sum = Integer.parseInt(c);
        return sum;
    }
}
